from django.contrib import admin
import os
from .models import *
# from .models import User
from .tools import logger, root_path

# Register your models here.

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ['name', 'id', 'mobile', 'city']

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'id', 'count', 'img']

@admin.action(description="published")
def make_published(modeladmin, request, queryset):
    queryset.update(status="p")


@admin.register(Content)
class ContentAdmin(admin.ModelAdmin):
    list_display = ['name', 'id', 'category', 'img', 'audio', 'video', 'video_width', 'video_height', 'vip', 'show', 'read_count']

    def save_model(self, request, obj, form, change):
        from moviepy.editor import VideoFileClip
        # 保存前
        super().save_model(request, obj, form, change)
        # 保存后
        # 获取视频的尺寸信息
        name = obj.name
        content = Content.objects.get(name = name)
        videoPath = os.path.join(root_path, content.video.path)
        clip = VideoFileClip(videoPath)
        width, height = clip.size
        content.video_width = width
        content.video_height = height
        content.save()

    actions = [make_published]

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ['id', 'money', 'status']